<?php

session_start();

include_once '../model/model_sql.php';



class controleurFormulaire {

    var $nom = "";
    var $prenom = "";
    var $dateNaissance = "";
    var $email = "";
    var $password = "";
    var $erreur = false;

    function __construct() {
        
    }

    function testNom($champs) {

        if (isset($champs)) {
            if (!strpos($champs, " ")) {

                if (preg_match('`^[- a-zàâäéèêëïîôöùûü\']{2,}$`i', $champs)) {
                    $messageErreur = "";
                } else {
                    $messageErreur = "ce champ contient des caractere speciaux";
                    $erreur = true;
                }
            } else {

                $messageErreur = "ce champs contient des espaces";
                $erreur = true;
            }
        }
        return $messageErreur;
    }

    function testEmail($champs) {

        if (isset($champs)) {
            if (!strpos($champs, " ")) {

                if (filter_var($champs, FILTER_VALIDATE_EMAIL)) {
                    $messageErreur = "";
                    $SQLBlog = new SQLBlog();
                    if ($SQLBlog->VerificationUtilisateurExistant($champs)) {
                        $messageErreur = "Cette adresse mail est deja utilisé";
                        $erreur = true;
                    }
                } else {
                    $messageErreur = "cette adresse mail n'est pas valide";
                    $erreur = true;
                }
            } else {
                $messageErreur = "ce champs contient des espaces";
                $erreur = true;
            }
        }
        return $messageErreur;
    }

    function testPassword($champs) {

        if (isset($champs)) {
            if (!strpos($champs, " ")) {
                $messageErreur = "";
            } else {
                $messageErreur = "ce champs contient des espaces";
                $erreur = true;
            }
        }
        return $messageErreur;
    }

    public function getNom() {
        return $this->nom;
    }

    public function getPrenom() {
        return $this->prenom;
    }

    public function getDateNaissance() {
        return $this->dateNaissance;
    }

    public function getEmail() {
        return $this->email;
    }

    public function getPassword() {
        return $this->password;
    }

    public function getErreur() {
        return $this->erreur;
    }

    public function setMessageErreur($messageErreur) {
        $this->messageErreur = $messageErreur;
    }

    public function setNom($nom) {
        $this->nom = $nom;
    }

    public function setPrenom($prenom) {
        $this->prenom = $prenom;
    }

    public function setDateNaissance($dateNaissance) {
        $this->dateNaissance = $dateNaissance;
    }

    public function setEmail($email) {
        $this->email = $email;
    }

    public function setPassword($password) {
        $this->password = $password;
    }

    public function setErreur($erreur) {
        $this->erreur = $erreur;
    }

}



if($_POST && !empty($_POST)) {

	
    if($_POST['captcha'] != $_SESSION['digit']){ 
		echo "Sorry, the CAPTCHA code entered was incorrect!";
		session_destroy();
		header("Location:../vue/FormulaireInscription.php?error=1");
	}
	else{
		$nom = "";
		if (isset($_POST['nom'])) {
			$nom = $_POST['nom'];
		}
		$prenom = "";
		if (isset($_POST['prenom'])) {
			$prenom = $_POST['prenom'];
		}
		$dateNaissance = "";
		if (isset($_POST['dateNaissance'])) {
			$dateNaissance = $_POST['dateNaissance'];
		}
		$email = "";
		if (isset($_POST['email'])) {
			$email = $_POST['email'];
		}
		$password = "";
		if (isset($_POST['password'])) {
			$password = $_POST['password'];
		}

		$SQLBlog = new SQLBlog();
		$controleur = new controleurFormulaire();
		$messageEnom = $controleur->testNom($nom);
		$messageEprenom = $controleur->testNom($prenom);
		$messageEemail = $controleur->testEmail($email);
		$messageEpassword = $controleur->testPassword($password);
		$dateDeNaissanceFormat = date("Y-m-d H:i:s", strtotime($dateNaissance));
		if ($controleur->getErreur() == false) {
			$SQLBlog->ajouterUtilisateur($email, $password, $nom, $prenom, $dateDeNaissanceFormat);
			header("Location:../vue/index.php");
		} else {
			header("Location:../vue/FormulaireInscription.php?messageEnom = $messageEnom&messageEprenom = $messageEprenom&messageEemail = $messageEemail&messageEPassword = $messageEpassword&nom = $nom&prenom = $prenom&dateDeNaissance = $dateNaissance&email = $email");
		}
	}
}
	
?>

